//
//  DataParser.m
//  Audian
//
//  Created by BXChinh on 12/13/13.
//  Copyright (c) 2013 Vũ Văn Long. All rights reserved.
//

#import "DataParser.h"

@implementation DataParser
/**
 Check status response
 */

+(BOOL)isResponseSuccess:(id)json{
    BOOL result = NO;
    if ([json isKindOfClass:[NSDictionary class]]) {
        
        if([[json valueForKey:@"StatusCode"] integerValue] == 0){
            result =YES;
        }
    }
    
    return result;
}

+ (id )parseDataResponse:(NSData*)aData{
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableLeaves error:&error];
    if(error != nil){
        NSLog(@"Parse error: %@", [error description]);
        return nil;
    } else {
        if ([json isKindOfClass:[NSDictionary class]]) {
            NSMutableDictionary* dict = [NSMutableDictionary dictionary];
            //if([DataParser isResponseSuccess:json]){
                dict = json;
            //}
            return dict;
        }else if([json isKindOfClass:[NSArray class]]){
             NSMutableArray* arr = [NSMutableArray array];
            arr = json;
            return arr;
        }
        return nil;
    }
}

+(id)jsonFromData:(NSData*)aData{
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:aData options:NSJSONReadingMutableLeaves error:&error];
    if(error != nil){
        return nil;
    }else{
        return json;
    }
}
@end
